package com.baseweb.webadmin.core.util;

import org.joda.time.DateTime;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class ObjectFormatter {
    public static String object2String(Object o) {
        return object2String(o, null);
    }

    public static String object2String(Object o, String defaultString) {
        defaultString = defaultString == null ? "" : defaultString;
        return o == null ? defaultString : o.toString();
    }

    public static Integer formatNumber(Integer n) {
        return n == null ? 0 : n;
    }

    public static Long formatNumber(Long n) {
        return n == null ? 0 : n;
    }

    public static Double formatNumber(Double n) {
        return n == null ? 0 : n;
    }

    public static Float formatNumber(Float n) {
        return n == null ? 0 : n;
    }

    public static String formatDateTime(DateTime d, String pattern) {
        pattern = pattern == null ? "yyyy-MM-dd HH:mm:ss" : pattern;
        return d == null ? "" : d.toString(pattern);
    }

    public static String formatDateTime(DateTime d) {
        return formatDateTime(d, null);
    }


    public static BigDecimal str2BigDecimal(String value){
        DecimalFormat df = new DecimalFormat("0.00");
        String str = df.format(Double.parseDouble(value)); // 先将Object 转化成 String

        return new BigDecimal(str);
    }

    public static void main(String[] args) {
        Integer t = 1;
        System.out.println(str2BigDecimal("2000").multiply(new BigDecimal(t)));
    }
}
